<style lang="stylus" scoped>
bgBlue = linear-gradient(to right, #04befe, #4481eb);

nav {
    width: 100%;
    height: 50px;
    background: bgBlue;

    img {
        display: inline-block;
        float: left;
        width: 25px;
        height: 25px;
        margin: 12px 10px;
    }

    p {
        display: inline-block;
        width: 80%;
        float: left;
        font-size: 18px;
        color: #ffffff;
        text-align: center;
        line-height: 50px;
    }
}

img {
    display: block;
    margin: 10rem auto 3rem auto;
    width: 200px;
}

button {
    width: 92%;
    margin: 2rem 4% 5rem 4%;
    background: bgBlue;
    color: #fff;
}

.withbox {
    padding: 0 20px;
    font-size: 16px;
    color: #333;
    text-align: center;
    line-height: 2.5;

    .p1 {
        font-size: 24px;
        color: #000;
    }

    .box1 {
        text-align: left;
    }

    .note {
        font-size: 14px;
        color: #999;
        line-height: 1.8;
    }
}
</style>
<template>
    <div>
        <nav>
                <img @click="onClose" src="./imgs/icon-left.png" alt="">
                <p>提现申请</p>
        </nav>
        <!-- <img src="./imgs/bg17.png" alt=""> -->
        <div class="withbox">
        <p>可提现金额</p>
        <p class="p1">{{beforMoney}}</p>
        <div class="box1">
            提现金额：<br/>
            <Input  placeholder="¥" v-model="money" style="width: 200px"/>
            <p class="note" v-if="money==''">单次提现必须大于120.00元</p>
            <p class="note" v-if="money!=''">结算服务费（提现金额的20.00%）<font style="float:right">{{(money/100*20).toFixed(2)}}元</font></p>
            <p class="note" v-if="money!=''">清算手续费（暂免）<font style="float:right">0.00元</font></p>
            <p class="note" v-if="money!=''">到账金额<font style="float:right">{{(money-(money/100*20)).toFixed(2)}}元</font></p>
            默认到账：<br/>
            <RadioGroup @on-change="onSelect" v-model="checkedValue">
                <Radio label="银行卡" ></Radio>
                <Radio label="微信" ></Radio>
            </RadioGroup>

            <div v-show="weixin">
              <span style="color:red">*</span> 微信账号：<br/>
              <Input  placeholder="" v-model="wxzh" style="width: 200px"/><br/>
             </div>
            <div v-show="bank">
              <span style="color:red">*</span> 姓名：<br/>
              <Input  placeholder="" v-model="userName" style="width: 200px"/><br/>

              <span style="color:red">*</span> 银行名称：<br/>
              <Input  placeholder="" v-model="bankName" style="width: 200px"/><br/>

              <span style="color:red">*</span> 银行卡号：<br/>
              <Input  placeholder="" v-model="bankCardNum" style="width: 200px"/><br/>
            </div>





        </div>
        <Button @click="handleClickTixian">立即提现</Button>
        <p class="note">每日提现到账时间点<br>11:00 18:00</p>
        </div>
    </div>
</template>
<script>
import axios from 'axios'
export default {
  name: "withdraw",
  data () {
    return {
      weixin:false,
      bank:true,
      checkedValue:"银行卡",
      money:'',
      wxzh:'',
      userName:'',
      bankName:'',
      bankCardNum:'',
      moneyType:'',
      beforMoney:'',
      txType:'',
    }
  },
  mounted(){
    if(!this.$store.state.show){
      this.$router.push({path: '/repayment'});
    }
    this.moneyType = this.$route.query.moneyType
    if(this.moneyType == "useCardBonus"){//消费神器金额
      this.txType = 13
      this.beforMoney = this.$store.state.user.useCardBonus
    }else if(this.moneyType == "userBonus"){//合伙人页面金额
      this.txType = 12
      this.beforMoney = this.$store.state.user.userBonus
    }else if(this.moneyType == "actual"){//我的余额
      var _this = this;
    axios
      .post("web/api/user/expectedReturn", {
        "userId":_this.$store.state.user.userId
      })
      .then(function(res) {
        var data = res.data.date;
        _this.beforMoney = data.actualIncome;
        _this.txType = 11
      })
      .catch(function(error) {
        console.log(error);
      });
    }else{
      this.txType = 11
      this.beforMoney=0;
    }

  },
  methods:{
    onClose(){
      this.$router.back(-1)
    },
    onChangeMoney(){
     this.money
    },
    onSelect(value){
      if(value=="银行卡"){
        this.bank = true
        this.weixin = false
      }else{
        this.bank = false
        this.weixin = true
      }

      console.log('this.bank', this.bank)
    },
    handleClickTixian(){
      if(this.money == ""){
        this.$Message.error("请输入提现金额");
        return;
      }

      if (this.money<120) {
        this.$Message.error("单次提现必须大于120.00元");
        return;
      }

      if(this.bank){
        if(this.userName == ""){
          this.$Message.error("请输入姓名");
          return;
        }
        if(this.bankName == ""){
          this.$Message.error("请输入银行名称");
          return;
        }
        if(this.bankCardNum == ""){
          this.$Message.error("请输入银行卡号");
          return;
        }
        //验证银行卡号
        if(this.bankCardNum.length < 16 || this.bankCardNum.length > 19) {
          this.$Message.error("银行卡号长度必须在16到19之间");
          return;
        }
        var num = /^\d*$/;
        if(!num.exec(this.bankCardNum)) {
          this.$Message.error("银行卡号必须全为数字");
          return;
        }
        var strBin = "10,18,30,35,37,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,58,60,62,65,68,69,84,87,88,94,95,98,99";
        if(strBin.indexOf(this.bankCardNum.substring(0, 2)) == -1) {
          this.$Message.error("银行卡号开头6位不符合规范");
          return;
        }
      }else{
        if(this.wxzh == ""){
          this.$Message.error("请输入微信账号");
          return;
        }
      }
      console.log("this.money",this.money);
      console.log("this.beforMoney",this.beforMoney);
      if (this.money>this.beforMoney) {
        this.$Message.error("输入金额不可大于可提现金额");
        return;
      }





      axios.post('/web/api/user/cashEarnings',{
          "userId":this.$store.state.user.userId,
          "cashEarningsMoney":this.money,
          "txType":this.txType,
          }).then(this.handleAddInfoSucc)
    },
    handleAddInfoSucc(res){
      var data = res.data
      console.log('tag',data)
      this.$Message.success(data.message)

      axios.post('/web/api/user/addShipLog',{
        "userId":this.$store.state.user.userId,
        "bankName":this.bankName,
        "userName":this.userName,
        "cardId":this.bankCardNum,
        "money":this.money,
        "txType":this.txType,
        }).then(this.addShipLogSucc)


    },
    addShipLogSucc(res){

      console.log('addShipLogSucc',res)
      axios.post('/web/api/user/users',{
        "pageSize":1,
        "pageNumber":1,
        "username":this.$store.state.user.username,
        "phone":""
        }).then(this.getListInfoSucc)
    },
    getListInfoSucc (res) {
      console.log('tag', res)
      var user = res.data.date.list[0]
      console.log('user===', user)
        this.$store.state.user = user;
        console.log('存储的user', this.$store.state.user)
        this.$router.push({path: '/'});

    },
  }
};
</script>
